package com.auxgroup.smarthome.inner.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auxgroup.bridge.app.inner.vo.DeviceModelInfoVo;
import com.auxgroup.bridge.app.inner.vo.DeviceModelListVo;
import com.auxgroup.bridge.app.inner.vo.DeviceModelSimpleVo;
import com.auxgroup.smarthome.inner.dto.DeviceModelInfoDto;
import com.auxgroup.smarthome.inner.entity.DeviceSnModelMappingEntity;
import com.auxgroup.smarthome.inner.service.DeviceModelInfoService;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.utils.common.LOG;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by niuGuangzhe on 2017/7/25.
 */
@Api(description = "型号相关操作", tags = "设备型号相关")
@RestController
public class DeviceModelInfoController {

    @Value("${mes.errPath}")
    private String MES_ERROR_PATH;

    @Autowired
    private DeviceModelInfoService deviceModelInfoService;

    @ApiOperation(value = "查询型号信息", notes = "型号管理--查询型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "型号名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "modelItem", value = "内机物料描述", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码(从第0页开始)", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/models", method = RequestMethod.GET)
    public ApiResponse<PageContent<DeviceModelInfoVo>> getDeviceInfo(@RequestParam(required = false) String model,
                                                                     @RequestParam(required = false) String modelItem,
                                                                     @RequestParam(defaultValue = "0", required = false) Integer page,
                                                                     @RequestParam(defaultValue = "10", required = false) Integer size) {

        Page<DeviceModelInfoVo> modelInfoPage = deviceModelInfoService.getDevicesInfo(model, modelItem, page, size);
        return ApiResponse.ok(new PageContent(modelInfoPage));
    }

    @ApiOperation(value = "查询型号信息-根据型号查找", notes = "型号管理--根据型号查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "型号名称", paramType = "query", dataType = "string")
    })
    @RequestMapping(value = "/model", method = RequestMethod.GET)
    public ApiResponse<DeviceModelInfoVo> getDeviceInfo(@RequestParam("model") String model) {
        return deviceModelInfoService.getDeviceInfoByModel(model);
    }

    @ApiOperation(value = "根据内机物料查询型号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelItem", value = "内机物料", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "integer", required = false),
            @ApiImplicitParam(name = "size", value = "分页大小", paramType = "query", dataType = "integer", required = false)
    })
    @GetMapping(value = "/model/model_item")
    public ApiResponse<DeviceModelInfoVo> getDeviceInfoByModelItem(@NotBlank(message = "modelItem不能为空") String modelItem) {
        return deviceModelInfoService.getDeviceInfoByModelItem(modelItem);
    }


    @ApiOperation(value = "查询型号信息-根据主键查找", notes = "型号管理--根据主键查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号主键", paramType = "query", dataType = "string")
    })
    @RequestMapping(value = "/model_by_id", method = RequestMethod.GET)
    public ApiResponse<DeviceModelInfoVo> getDeviceInfoByModelId(String modelId) {
        return deviceModelInfoService.getDeviceInfoByModelId(modelId);
    }

    @ApiOperation(value = "直接返回所有的型号名称以及主键", notes = "型号管理--返回所有型号")
    @RequestMapping(value = "/all_model", method = RequestMethod.GET)
    public ApiResponse<List<DeviceModelSimpleVo>> getAllModelInfo() {
        return deviceModelInfoService.getAllNewDeviceModel();
    }

    @ApiOperation(value = "所有型号列表", notes = "型号管理--返回所有型号,app使用")
    @GetMapping(value = "/all_model_app")
    public ApiResponse<List<DeviceModelListVo>> getAllModelInfoToApp() {
        return deviceModelInfoService.getAllModelInfo();
    }

    @ApiOperation(value = "是否存在型号名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model",value = "型号名称",paramType = "query",dataType = "string",required = true)
    })
    @GetMapping(value = "/is_exists_model_name")
    public boolean isExistsModelName(String model) {
        return deviceModelInfoService.isExistsModelName(model);
    }

    @ApiOperation(value = "新增型号信息", notes = "型号管理--新增型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "型号", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "modelItem", value = "内机物料描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "deviceModelInfoDto", value = "设备型号传输对象[已序列化]", paramType = "form", dataType = "string", required = true),
    })
    @RequestMapping(value = "/model", method = RequestMethod.POST)
    public ApiResponse addDeviceInfo(String model, String modelItem,
                                     String deviceModelInfoDto) {

        if (StringUtils.isBlank(model) || StringUtils.isBlank(modelItem)) {
            return ApiResponse.prompt(Syscode.DEVICE_MODEL_INPUT_ILLEGAL);
        }
        DeviceModelInfoDto dto = JSONObject.parseObject(deviceModelInfoDto, DeviceModelInfoDto.class);
        return deviceModelInfoService.addDeviceInfo(dto, model, modelItem);
    }

    @ApiOperation(value = "修改型号信息", notes = "型号管理--修改型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号主键", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "deviceModelInfoDto", value = "设备型号传输对象[已序列化]", paramType = "form", dataType = "string", required = true),
    })
    @RequestMapping(value = "/model", method = RequestMethod.PUT)
    public ApiResponse updateDeviceInfo(@RequestParam(value = "modelId") String modelId,
                                        String deviceModelInfoDto) {
        if (StringUtils.isBlank(modelId)) {
            return ApiResponse.prompt(Syscode.DEVICE_MODEL_INPUT_ILLEGAL);
        }
        DeviceModelInfoDto dto;
        try {
            dto = JSONObject.parseObject(deviceModelInfoDto, DeviceModelInfoDto.class);
        } catch (Exception e) {
            LOG.error(this, "JSON无法解析", e);
            return ApiResponse.prompt(Syscode.DEVICE_MODEL_JSON_ILLEGAL);
        }
        return deviceModelInfoService.updateDeviceInfo(dto, modelId);
    }

    @ApiOperation(value = "删除型号信息", notes = "型号管理--删除型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号主键", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/model", method = RequestMethod.DELETE)
    public ApiResponse deleteDeviceInfo(String modelId) {
        if (StringUtils.isBlank(modelId)) {
            return ApiResponse.prompt(Syscode.DEVICE_MODEL_INPUT_ILLEGAL);
        }
        return deviceModelInfoService.deleteDeviceInfo(modelId);
    }

    @ApiOperation(value = "扫描sn码获取设备信息", notes = "型号管理扩展-扫描sn码获取设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备sn码", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/device_model", method = RequestMethod.GET)
    public ApiResponse<DeviceModelInfoVo> getDeviceModelBySn(String sn) {
        if (StringUtils.isBlank(sn)) {
            return ApiResponse.prompt(Syscode.DEVICE_MODEL_INPUT_ILLEGAL);
        }
        return deviceModelInfoService.findDeviceTypeBySn(sn);
    }

    @ApiOperation(value = "获取物料描述信息", notes = "型号管理扩展--获取物料描述")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "物料关键字（不传默然返回所有物料信息）", paramType = "query", dataType = "string")
    })
    @RequestMapping(value = "/model_items", method = RequestMethod.GET)
    public ApiResponse<List<String>> getDeviceItems(@RequestParam(required = false) String keyword,
                                                    @RequestParam(defaultValue = "0", required = false) Integer page,
                                                    @RequestParam(defaultValue = "100", required = false) Integer size) {
        return deviceModelInfoService.findKeyModelItem(keyword,page,size);
    }

    //@ApiIgnore
    @ApiOperation(value = "获取推送sn数据", notes = "导入型号Sn映射数据并入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "需要拉取的日期，格式严格按照yyyy-MM-dd", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/batch_sn_mapping", method = RequestMethod.POST)
    public ApiResponse batchSavePushSn(@RequestParam(value = "content") String content) {
        List<DeviceSnModelMappingEntity> deviceSnModelMappingEntityListMes =
                JSON.parseArray(content, DeviceSnModelMappingEntity.class);
        if(deviceSnModelMappingEntityListMes.size() > 0){
            deviceModelInfoService.writeDataToDB(deviceSnModelMappingEntityListMes);
        }
        return ApiResponse.prompt(Syscode.SC_OK);
    }

    /**
     *
     * @param modelId
     * @return
     */
    @ApiOperation(value = "计算modelId的Sn匹配数量")
    @GetMapping(value = "/count_sn_by_model_id")
    public long countSnByModelId(@RequestParam("modelId") String modelId, @RequestParam("sn") String sn) {
        return deviceModelInfoService.countSnByModelId(modelId, sn);
    }


}
